<?php

/**
 * Admin_Login File
 * 
 * Short file description
 *
 * PHP Version: 5.3.26
 * 
 * @category Admin_Login
 * @package  Admin_Login_Package
 * @author Johari <joharijumali@gmail.com>  
 */

use Admin\Models\Admin;

/**
 * Admin_Login class
 * 
 * Admin Login Class
 *
 * @category Admin_Login_Controller
 * @package  Admin_Login_Package
 * @author Johari <joharijumali@gmail.com>   
 */

class Admin_Login_Controller extends Controller
{

    public $restful = true;

	/**
     * Function __construct
	 *
	 * @return __construct.
     */
    public function __construct()
	{
        parent::__construct();

        Config::set('auth.driver', 'adminauth');

        Asset::container('header')->bundle('admin');
        Asset::container('header')->add('bootstrap', 'css/bootstrap.min.css');
    }
	
	/**
     * Function get_index
	 *
	 * @return get_index.
     */
    public function get_index()
	{
        $detect = new Mobile_Detect;

        if ($detect->isMobile() || $detect->isTablet()) {
            $data['loginlayout'] = 'admin::layouts.m_admin';
        } else {
            $data['loginlayout'] = 'admin::layouts.admin';  
        }

        return View::make('admin::login.index', $data);
    }

    /**
     * Function post_index
     *
     * @return post_index.
     */
    public function post_index()
    {
        $creds = array(
            'username' => Input::get('username'),
            'password' => Input::get('password'),
        );

        if (Auth::attempt($creds)) {
            return Redirect::to(URL::to_action('admin::home@dashboard'));
        } else {
            return Redirect::back()->with('error', true);
        }
    }

	/**
     * Function post_credentials
	 *
	 * @return post_credentials.
     */
    public function post_credentials()
	{
        $creds = Config::get('application.key');

        $allAdmin = Admin::all();

        $name = Str::limit_exact(Str::upper(Str::random(16)), 16);
        $pword = Str::limit_exact(Str::upper(Str::random(16,'alpha')), 16);

        foreach ($allAdmin as $value) {
            
            Admin::where('credential', '=', $creds)->update(array('username' => $name,'password' => Hash::make($pword)));

            try {
                $mailer = Message::to('joharijumali@gmail.com');
                $mailer->from('admin@3fresorces.com', 'Admin Request Credential');
                $mailer->subject(Str::title(__('admin.subjectconfirm')));
                $mailer->body('view: admin::plugin.email');
                $mailer->body->username = $name;
                $mailer->body->name = $value->name;
                $mailer->body->password = $pword;
                $mailer->body->key = '';
                $mailer->html(true);
                $mailer->send();

                Log::write('email', 'Message was to itbmwebtas@gmail.com was sent.');
            } catch (Exception $e) {
                Log::write('email', 'Message was to itbmwebtas@gmail.com not sent.');
                Log::write('email', 'Mailer error: '.$e->getMessage());
            }



        }
    }
	
	/**
     * Function get_logout
	 *
	 * @return get_logout.
     */
    public function get_logout()
	{
        Auth::logout();

        $creds = Config::get('application.key');

        $allAdmin = Admin::all();

        $name = Str::limit_exact(Str::upper(Str::random(16)), 16);
        $pword = Str::limit_exact(Str::upper(Str::random(16,'alpha')), 16);

        foreach ($allAdmin as $value) {
            
            Admin::where('credential', '=', $creds)->update(array('username' => $name,'password' => Hash::make($pword)));

        }

        return Redirect::to(URL::to_action('admin::home@index'));
    }

}

?>